<template>
  <div>
    <el-form ref="dataForm" :model="form" label-width="100px">
      <div class="ledgerTitle">计划分工</div>
      <el-row>
        <el-col :span="6">
          <el-form-item label-width="140px" label="被审计单位:" prop="auditedCompanyName">
            {{ form.auditedCompanyName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="110px" label="被审计个人:" prop="auditedUserNames">
            {{ form.auditedUserNames }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="110px" label="审计类型:" prop="auditType">
            <DictTag :options="dict.type.audit_type" :value="form.auditType"></DictTag>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="100px" label="是否披露:" prop="isPublish">
            <div v-if="Number(form.isPublish) === 0">
              <span>否</span>
            </div>
            <div v-else>
              <span>是</span>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="form.auditType === 1">
        <el-col :span="12">
          <el-form-item label-width="140px" label="任职区间:" prop="tenureDates">
            <span v-if="form.tenureStartDate" style="margin-right: 5px;">{{ form.tenureStartDate }} 至</span>
            <span style="margin-left: 5px;">{{ form.tenureEndDate }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="110px" label="审计子类型:" prop="auditTypeSub">
            {{form.auditTypeSub}}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label-width="140px" label="审计区间:" prop="auditDates">
            <span v-if="form.auditStartDate" style="margin-right: 5px;">{{ form.auditStartDate }} 至</span>
            <span style="margin-left: 5px;">{{ form.auditEndDate }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label-width="110px" label="审计目的:" prop="auditPurpose">
            {{ form.auditPurpose }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label-width="140px" label="审计范围:" prop="auditScope">
            <pre class="pre-style">{{ form.auditScope }}</pre>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="ledgerTitle">审计分工</div>
      <el-row>
        <el-col :span="6">
          <el-form-item label-width="140px" label="审计组组长:" prop="auditLeader">
            {{ form.auditLeader }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="110px" label="协审单位:" prop="assistCompanyName">
            {{ form.assistCompanyName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label-width="110px" label="协审单位组长:" prop="assistLeader">
            {{ form.assistLeader }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label-width="140px" label="被审计单位联络员:" prop="auditedCompanyUserName">
            {{ form.auditedCompanyUserName }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="110px" label="协审单位主审:" prop="assistJudge">
            {{ form.assistJudge }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label-width="110px" label="审计组成员:" prop="assistMembers">
            {{ form.assistMembers }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label-width="140px" label="计划工作时间:" prop="projectDates">
            <span v-if="form.projectStartDate" style="margin-right: 5px;">{{ form.projectStartDate }} 至</span>
            <span style="margin-left: 5px;">{{ form.projectEndDate }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label-width="110px" label="实施工作时间:" prop="workDates">
            <span v-if="form.workStartDate" style="margin-right: 5px;">{{ form.workStartDate }} 至</span>
            <span style="margin-left: 5px;">{{ form.workEndDate }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="ledgerTitle">审计项目文书</div>
      <div class="inputBox" v-if="!management">
        <div class="inputBox">
          <el-input :readonly="true" v-model="form.projectName"></el-input>
          <ul class="fileBar" v-if="form.wordFile">
            <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
            <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
          </ul>
        </div>
      </div>
      <el-tabs type="border-card" v-if="management" v-model="form.activeName" @tab-click="handleTabClick">
        <el-tab-pane label="审计项目" name = "project">
          <div class="ledgerTitle">审计项目</div>
          <el-row class="inputBox" v-loading="loading">
            <div class="inputBox">
              <el-input :readonly="true" v-model="form.projectName"></el-input>
              <ul class="fileBar" v-if="form.wordFile">
                <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
              </ul>
            </div>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="工作实施方案" name = "apply" :disabled="this.form.projectProgress < 2">
          <div class="ledgerTitle">工作实施方案</div>
          <el-row class="inputBox" v-loading="loading">
            <div class="inputBox">
              <el-input :readonly="true" v-model="form.projectName"></el-input>
              <ul class="fileBar" v-if="form.wordFile">
                <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
              </ul>
            </div>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="审计通知书" name = "notice" :disabled="this.form.projectProgress < 2">
          <div class="ledgerTitle">审计通知书</div>
          <el-form v-loading="loading">
            <el-row class="inputBox">
              <div class="inputBox">
                <el-input :readonly="true" v-model="form.projectName"></el-input>
                <ul class="fileBar" v-if="form.wordFile">
                  <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                  <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
                </ul>
              </div>
            </el-row>
            <div class="ledgerTitle">资料清单</div>
            <el-row>
              <el-col>
                <el-table
                  ref="table"
                  :data="noticeTableData"
                  :model="noticeTableData"
                  max-height="450"
                  highlight-current-row
                >
                  <el-table-column label="序号" width="100">
                    <template #default="{ $index }">
                      <span>{{ $index + 1 }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="必传" width="100">
                    <template #default="{ row }">
                      <el-tag :type="row.required | tureOrFalseTag2">{{ row.required | tureOrFalse }}</el-tag>
                    </template>
                  </el-table-column>
                  <el-table-column label="资料名称">
                    <template #default="{ row }">
                      <pre class="pre-style">{{ row.name }}</pre>
                    </template>
                  </el-table-column>
                  <el-table-column label="份数" width="100">
                    <template #default="{ row }">
                      {{ row.copies }}
                    </template>
                  </el-table-column>
                  <el-table-column label="被审计单位上传资料" width="280">
                    <template #default="{ row }">
                      <el-upload
                        class="upload-demo"
                        action="/admin/sys-file/upload"
                        :on-preview="handlePreview"
                        :show-file-list="true"
                        :file-list="row.fileList">
                      </el-upload>
                    </template>
                  </el-table-column>
                  <el-table-column label="备注" width="280">
                    <template #default="{ row }">
                      <pre class="pre-style">{{ row.remark }}</pre>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="疑点审计证据" name = "doubtfulAttachment" :disabled="this.form.projectProgress < 3">
          <div class="ledgerTitle">疑点审计证据</div>
            <el-form v-loading="loading">
            <el-row>
              <el-col>
                <el-table
                  ref="table"
                  :data="doubtfulTableData"
                  :model="doubtfulTableData"
                  style="width: 100%"
                  highlight-current-row
                >
                  <el-table-column label="序号" width="50">
                    <template #default="{ row, $index }">
                      <span>{{ $index+1}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="证据类别">
                    <template #default="{ row }">
                      <DictTag :options="dict.type.evidence_type" :value="row.evidenceType"></DictTag>
                    </template>
                  </el-table-column>
                  <el-table-column label="取证日期">
                    <template #default="{ row }">
                      {{row.evidenceDate}}
                    </template>
                  </el-table-column>
                  <el-table-column label="相关附件">
                    <template #default="{ row }">
                      <el-upload
                        class="upload-demo"
                        action="/admin/sys-file/upload"
                        :on-preview="handlePreview"
                        :show-file-list="true"
                        :file-list="row.fileList">
                      </el-upload>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="审计取证单" name = "evidence" :disabled="this.form.projectProgress < 3">
          <div class="ledgerTitle">审计取证单</div>
          <el-form v-loading="loading">
            <el-row>
              <el-col>
                <el-table
                  ref="table"
                  :data="evidenceTableData"
                  :model="evidenceTableData"
                  style="width: 100%"
                  highlight-current-row
                >
                  <el-table-column label="序号" width="50">
                    <template #default="{ row, $index }">
                      <span>{{ $index+1 }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="取证单状态" width="150">
                    <template #default="{ row }">
                      <span v-if="row.status === 0 || row.status === null">新建</span>
                      <span v-else-if="row.status === 1">审核中</span>
                      <span v-else-if="row.status === 2">驳回</span>
                      <span v-else-if="row.status === 3">审核通过待答复</span>
                      <span v-else-if="row.status === 4">已答复待转交审计组</span>
                      <span v-else-if="row.status === 5">已转交审计组</span>
                      <span v-else-if="row.status === 6">已生成问题</span>
                      <span v-else-if="row.status === 7">已关闭</span>
                      <span v-else>未知状态</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="取证单文书">
                    <template #default="{ row }">
                      <div class="inputBox">
                        <div class="inputBox">
                          <el-input :readonly="true" v-model="row.code"></el-input>
                          <ul class="fileBar" v-if="row.wordFile">
                            <li @click="openWord(row.wordFile)"><i class="el-icon-search"></i></li>
                            <li @click="downWord(row.wordFile)"><i class="el-icon-download"></i></li>
                          </ul>
                        </div>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column label="附件" width="280">
                    <template #default="{ row }">
                      <el-upload
                        class="upload-demo"
                        action="/admin/sys-file/upload"
                        :on-preview="handlePreview"
                        :show-file-list="true"
                        :file-list="row.fileList">
                      </el-upload>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="审计报告初稿" name = "opinion" :disabled="this.form.projectProgress < 4">
          <div class="ledgerTitle">审计报告初稿</div>
          <el-row class="inputBox" v-loading="loading">
            <div class="inputBox">
              <el-input :readonly="true" v-model="form.projectName"></el-input>
              <ul class="fileBar" v-if="form.wordFile">
                <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
              </ul>
            </div>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="征求意见书" name = "advice" :disabled="this.form.projectProgress < 4">
          <div class="ledgerTitle">征求意见书</div>
          <el-row class="inputBox" v-loading="loading">
            <div class="inputBox">
              <el-input :readonly="true" v-model="form.projectName"></el-input>
              <ul class="fileBar" v-if="form.wordFile">
                <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
              </ul>
            </div>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="审计报告终稿" name = "report" :disabled="this.form.projectProgress < 4">
          <div class="ledgerTitle">审计报告终稿</div>
          <el-row class="inputBox" v-loading="loading">
            <div class="inputBox">
              <el-input :readonly="true" v-model="form.projectName"></el-input>
              <ul class="fileBar" v-if="form.wordFile">
                <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
              </ul>
            </div>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="整改通知书" name = "rectifyNotice" :disabled="this.form.projectProgress < 4">
          <div class="ledgerTitle">整改通知书</div>
          <el-row class="inputBox" v-loading="loading">
            <div class="inputBox">
              <el-input :readonly="true" v-model="form.projectName"></el-input>
              <ul class="fileBar" v-if="form.wordFile">
                <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
              </ul>
            </div>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="整改报告" name = "rectifyReport" :disabled="this.form.projectProgress < 4">
          <div class="ledgerTitle">整改报告</div>
          <el-form v-loading="loading">
            <div class="inputBox">
              <div class="inputBox">
                <el-input :readonly="true" v-model="form.projectName"></el-input>
                <ul class="fileBar" v-if="form.wordFile">
                  <li @click="openWord(form.wordFile)"><i class="el-icon-search"></i></li>
                  <li @click="downWord(form.wordFile)"><i class="el-icon-download"></i></li>
                </ul>
              </div>
            </div>
            <div class="ledgerTitle">整改情况</div>
            <el-row>
              <el-col>
                <el-table
                  ref="table"
                  :data="rectifyReportTableData"
                  :model="rectifyReportTableData"
                  style="width: 100%"
                  highlight-current-row
                >
                  <el-table-column type="selection" width="55" align="center"></el-table-column>
                  <el-table-column label="序号" width="100">
                    <template #default="{ $index }">
                      <span>{{ $index + 1 }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="问题标题">
                    <template #default="{ row }">
                      <pre class="pre-style">{{ form.problemTitle }}</pre>
                    </template>
                  </el-table-column>
                  <el-table-column label="问题描述">
                    <template #default="{ row }">
                      <pre class="pre-style">{{ form.problemDesc }}</pre>
                    </template>
                  </el-table-column>
                  <el-table-column label="整改措施">
                    <template #default="{ row }">
                      <pre class="pre-style">{{ form.rectificationMeasures }}</pre>
                    </template>
                  </el-table-column>
                  <el-table-column label="完成情况" width="120">
                    <template #default="{ row }">
                      <DictTag :options="dict.type.ectification_situation" :value="row.rectifyStatus"></DictTag>
                    </template>
                  </el-table-column>
                  <el-table-column label="佐证资料" width="280">
                    <template #default="{ row }">
                      <el-upload
                        class="upload-demo"
                        action="/admin/sys-file/upload"
                        :on-preview="handlePreview"
                        :show-file-list="true"
                        :file-list="row.fileList">
                      </el-upload>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-form>
  </div>
</template>

<script>
import Preview from "@/views/gen/preview";
import {getProject} from "@/api/adis/project/project";
import {getApplyByProjectId} from "@/api/adis/project/projectApply";
import {getNoticeByProjectId} from "@/api/adis/project/notice";
import {getDoubtfulByProjectId} from "@/api/adis/execute/point";
import {getEvidenceByProjectId} from "@/api/adis/execute/evidence";
import {getReportOpinionByProjectId} from "@/api/adis/report/reportOpinion";
import {getAdviceByProjectId} from "@/api/adis/report/advice";
import {getReportFinalByProjectId} from "@/api/adis/report/reportFinal";
import {getRectifyReportByProjectId} from "@/api/adis/track/rectifyReprot";
import {getRectifyNoticeByProjectId} from "@/api/adis/track/rectify";

export default {
  name: 'ProjectAuditView',
  props: {
    id: {
      type: String,
      default: ''
    },
    params: {
      type: Object,
    },
    isAudit: {
      type: Boolean,
      default: false,
    },
    management: {
      type: Boolean,
      default: false,
    }
  },
  components: {Preview},
  dictTypes: ['audit_type', 'evidence_type', 'ectification_situation'],
  data() {
    return {
      form: {
        activeName: "project",
        prevActiveName: 'project',
      },
      loading: false,
      noticeTableData: [],
      doubtfulTableData: [],
      evidenceTableData: [],
      rectifyReportTableData: [],
    }
  },
  watch: {
    id: {
      handler(val){
        if (val) {
          getProject(val).then((res) => {
            if (res.data.data !== null){
              this.form = res.data.data;
              this.$emit('data-loaded', true);
            } else {
              this.$emit('data-loaded', false);
            }
          }).catch(() => {
            this.$emit('data-loaded', false);
          });
        }
      },deep: true, immediate: true
    },
    params: {
      handler(val) {
        if (val){
          this.form = val;
        }
      },deep: true, immediate: true
    },
  },
  methods: {
    handleTabClick(tab) {
      if (this.form.activeName !== this.form.prevActiveName) {
        this.loading = true;
        this.form.prevActiveName = this.form.activeName;
        this.resetData()
        if (!this.debouncedTabPaneClick) {
          this.debouncedTabPaneClick = this.debounce(this.tabPaneClick, 500);
        }
        this.debouncedTabPaneClick(tab);
      }
    },
    debounce(func, wait) {
      let timeout;
      return function(...args) {
        clearTimeout(timeout);
        timeout = setTimeout(() => func.apply(this, args), wait);
      };
    },
    tabPaneClick(tab){
      switch (tab.label) {
        case "审计项目":
          getProject(this.form.projectId).then((projectResponse) => {
            this.$set(this.form, "wordFile", projectResponse.data.data.wordFile);
            this.loading = false;
          });
          break;
        case "工作实施方案":
          getApplyByProjectId(this.form.projectId).then(applyResponse => {
            if (applyResponse.data.data){
              this.$set(this.form, "wordFile", applyResponse.data.data.wordFile);
            }
            this.loading = false;
          });
          break;
        case "审计通知书":
          getNoticeByProjectId(this.form.projectId).then((noticeResponse) => {
            if (noticeResponse.data.data){
              this.$set(this.form, "wordFile", noticeResponse.data.data.wordFile);
              this.noticeTableData = noticeResponse.data.data.noticeAttachments || [];
              this.noticeTableData.forEach(item => {
                item.fileList = [];
                item.attachments.forEach(col => {
                  item.fileList.push({name: col.fileName, url: col.fileUrl});
                })
              })
            }
            this.loading = false;
          });
          break;
        case "疑点审计证据":
          getDoubtfulByProjectId(this.form.projectId).then(doubtfulResponse => {
            if (doubtfulResponse.data.data){
              this.doubtfulTableData = doubtfulResponse.data.data  || [];
              this.doubtfulTableData.forEach(item => {
                item.fileList = [];
                item.attachments.forEach(col => {
                  item.fileList.push({name: col.fileName, url: col.fileUrl});
                })
              })
            }
            this.loading = false;
          });
          break;
        case "审计取证单":
          getEvidenceByProjectId(this.form.projectId).then((evidenceResponse) => {
            if (evidenceResponse.data.data){
              this.evidenceTableData = evidenceResponse.data.data || [];
              this.evidenceTableData.forEach(item => {
                item.fileList = [];
                item.attachments.forEach(col => {
                  item.fileList.push({name: col.fileName, url: col.fileUrl});
                })
              })
            }
            this.loading = false;
          });
          break;
        case "审计报告初稿":
          getReportOpinionByProjectId(this.form.projectId).then((opinionResponse)=>{
            if (opinionResponse.data.data){
              this.$set(this.form, "wordFile", opinionResponse.data.data.wordFile);
            }
            this.loading = false;
          });
          break;
        case "征求意见书":
          getAdviceByProjectId(this.form.projectId).then((adviceResponse) => {
            if (adviceResponse.data.data){
              this.$set(this.form, "wordFile", adviceResponse.data.data.wordFile);
            }
            this.loading = false;
          });
          break;
        case "审计报告终稿":
          getReportFinalByProjectId(this.form.projectId).then((finalResponse)=>{
            if (finalResponse.data.data){
              this.$set(this.form, "wordFile", finalResponse.data.data.wordFile);
            }
            this.loading = false;
          })
          break;
        case "整改通知书":
          getRectifyNoticeByProjectId(this.form.projectId).then((rectifyResponse)=>{
            if (rectifyResponse.data.data){
              this.$set(this.form, "wordFile", rectifyResponse.data.data.wordFile);
            }
            this.loading = false;
          });
          break;
        case "整改报告":
          getRectifyReportByProjectId(this.form.projectId).then((rectifyReportResponse)=>{
            if (rectifyReportResponse.data.data){
              this.$set(this.form, "wordFile", rectifyReportResponse.data.data.wordFile);
              this.rectifyReportTableData = rectifyReportResponse.data.data.rectifyReportProblems || [];
              this.rectifyReportTableData.forEach(item => {
                item.fileList = [];
                item.attachments.forEach(col => {
                  item.fileList.push({name: col.fileName, url: col.fileUrl});
                })
              })
            }
            this.loading = false;
          });
          break;
      }
    },
    resetData(){
      this.form.wordFile = '';
      this.noticeTableData = [];
      this.doubtfulTableData = [];
      this.evidenceTableData = [];
      this.rectifyReportTableData = [];
    },
    downWord(wordFile) {
      if(!wordFile) {
        this.$message({
          type: "warning",
          message: "该项尚未上传相关文书!"
        });
        return;
      }
      const a = document.createElement('a');
      let url = wordFile;
      a.setAttribute('download', '');
      a.setAttribute('href', url);
      a.click();
    },
    openWord(wordFile) {
      if(!wordFile) {
        this.$message({
          type: "warning",
          message: "该项尚未上传相关文书!"
        });
        return;
      }
      // this.$router.push({path:'/webOffice', query: {url: this.form.wordFile}})
      const { href } = this.$router.resolve({
        path: '/webOffice',
        query: {
          url: wordFile,
          type: this.isAudit ? 'edit' : 'view',
        }
      });
      window.open(href, '_blank');
    },

    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
    },
  }
}
</script>

